package com.hup.joystick.service.jna;

/**
 * DirectInputApi 使用时抛出的HResult异常
 *
 * @author hugan
 * @date 2020/10/16
 */
public class DInputHResultException extends Exception {

    @SuppressWarnings("SpellCheckingInspection")
    public enum HResult {
        S_OK(0, "S_OK", "成功"),
        E_FAIL(-2147467259, "E_FAIL", "错误"),
        DIERR_INVALID_PARAM(-2147024809, "DIERR_INVALIDPARAM", "参数错误"),
        DIERR_NOT_INITIALIZED(-2147024875, "DIERR_NOTINITIALIZED", "未初始化模块"),
        DIERR_OBJECT_NOT_FOUND(-2147024894, "DIERR_OBJECTNOTFOUND", "找不到对象"),
        DIERR_UNSUPPORTED(-2147467263, "DIERR_UNSUPPORTED", "不支持功能?"),
        /**
         * 选了有问题的设备,或不存在的设备
         */
        DIERR_DEVICE_NOT_REG(-2147221164, "DIERR_DEVICENOTREG", "设备未连接"),
        DIERR_NO_INTERFACE(-2147467262, "DIERR_NOINTERFACE", "非法接口?"),
        DIERR_OUT_OF_MEMORY(-2147024882, "DIERR_OUTOFMEMORY", "内存溢出"),
        E_POINTER(-2147467261, "E_POINTER", "无效指针"),
        DIERR_INPUT_LOST(-2147024866, "DIERR_INPUTLOST", "设备已断开"),
        DIERR_NOT_ACQUIRED(-2147024884, "DIERR_NOTACQUIRED", "不能访问设备?"),
        E_PENDING(-2147483638, "E_PENDING", "准备中?"),
        UNKNOWN(-1, "UNKNOWN", "未知错误")
        ;

        public final int val;
        public final String enMsg;
        public final String chMsg;

        HResult(int val, String enMsg, String chMsg) {
            this.val = val;
            this.enMsg = enMsg;
            this.chMsg = chMsg;
        }

        /**
         * @param hResult DirectInputApi的调用结果
         * @return 是否成功
         */
        public static boolean succeeded(int hResult) {
            return hResult >= 0;
        }

        /**
         * @param hResult DirectInputApi的调用结果
         */
        public static String getErrMsg(int hResult) {
            for (HResult hr : HResult.values()) {
                if (hr.val == hResult) {
                    return hr.chMsg;
                }
            }
            return "未知异常:" + hResult;
        }

        public static HResult getResult(int hResult) {
            for (HResult hr : HResult.values()) {
                if (hr.val == hResult) {
                    return hr;
                }
            }
            return UNKNOWN;
        }
    }

    public final int hResult;

    public DInputHResultException(int hResult) {
        super(HResult.getErrMsg(hResult));
        this.hResult = hResult;
    }

    public DInputHResultException(String desc, int hResult) {
        super(desc + "异常:" + HResult.getErrMsg(hResult));
        this.hResult = hResult;
    }

    public static void testThrow(String desc, int hResult) throws DInputHResultException {
        if (HResult.succeeded(hResult)) return;

        DInputHResultException err = new DInputHResultException(desc, hResult);
        //去掉本方法的记录
        StackTraceElement[] stackTrace = err.getStackTrace();
        StackTraceElement[] reset = new StackTraceElement[stackTrace.length - 1];
        System.arraycopy(stackTrace, 1, reset, 0, reset.length);
        err.setStackTrace(reset);
        throw err;
    }

}
